package com.blueflamedev.util
{
	import flash.display.Stage;
	import flash.events.EventDispatcher;
	import flash.events.KeyboardEvent;
	import flash.events.Event;
	
	/**
	 * <code>BlindPhrase</code> lets you assign a phrase that, when blindly typed, will trigger a <code>COMPLETE</code> event.
	 * @author Eric Smith
	 */	
	public class BlindPhrase extends EventDispatcher
	{
		/**
		 * When this phrase is typed, an event will be triggered. 
		 */		
		public var phrase:String;
		
		/**
		 * @private 
		 */		
		private var _stage:Stage;
		
		/**
		 * @private 
		 */		
		private var _index:int = 0;
		
		/**
		 * Initializes a new blindly typed phrase.
		 *  
		 * @param stage A reference to the stage
		 * @param phrase The phrase that you want to blindly type to trigger an event.
		 */		
		public function BlindPhrase(stage:Stage, phrase:String)
		{
			_stage = stage;
			this.phrase = phrase;
			
			_stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
		}
		
		/**
		 * @private
		 */		
		private function onKeyDown(event:KeyboardEvent):void
		{
			if (event.charCode == phrase.charCodeAt(_index))
			{
				if (_index == phrase.length -1)
				{
					dispatchEvent(new Event(Event.COMPLETE));
					_index = 0;
				} else
				{
					_index++;
				}
			} else
			{
				_index = 0;
			}
		}
		
	}
}